package store.lunangangster.nodeflow.rt.element.flow;

import cn.hutool.core.util.BooleanUtil;
import store.lunangangster.nodeflow.rt.DataBus;
import store.lunangangster.nodeflow.rt.element.Node;

/**
 * 循环执行流的抽象类，所有循环流都需要实现该抽象类
 * <p>该类主要定义了所有循环执行流的退出逻辑
 *
 * @author cwk
 * @version 1.0
 */
public abstract class LoopFlow extends Flow {

  private Node exitNode;

  /**
   * 每次循环结束时的退出逻辑判断
   */
  public boolean exit(Integer slotIndex) {
    if (exitNode == null) {
      return false;
    }
    try {
      exitNode.execute(slotIndex);
      return BooleanUtil.isTrue((Boolean) DataBus.getSlot(slotIndex)
          .getData(exitNode.getNodeComponent().getExitResultKey()));
    } finally {
      exitNode.getNodeComponent().removeExitResultKey();
    }
  }

  public Node getExitNode() {
    return exitNode;
  }

  public void setExitNode(Node exitNode) {
    this.exitNode = exitNode;
  }

}
